package project.euler.honk.problem.to50;

public final class Problem41 {

	private static boolean isPrime(int number) {
		if (number == 1)
			return false;
		int max = (int) Math.sqrt(number);
		for (int i = 3; i <= max; i+=2)
			if (number % i == 0)
				return false;
		return true;
	}

	private static boolean isPali(int value) {
		final boolean[] seen = new boolean[10];
		while (value > 0) {
			int c = value % 10;
			if (c == 0 || seen[c])
				return false;
			seen[c] = true;
			value /= 10;
		}
		boolean b = false;
		int x = 0;
		for (int i = 1; i < seen.length; i++) {
			if(!seen[i]) {
				if(x == 0) x = i;
				b = true;
			} else if(x != 0 || b){
				return false;
			}
		}
		return true;
	}
	
	public static int solve() {
		for (int i = 7654321; i > 0; i-=2) {
			if (isPali(i)) {
				if (isPrime(i)) {
					return i;
				}
			}
		}
		return 0;
	}
	
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		long result = solve();
		long end = System.currentTimeMillis();
		System.out.println("Result: " + result + " Time: " + (end - start));
	}

}
